import {Trigger} from '@/trigger';
import {Unit} from '@/unit';
import ActorItemUtil from "@/ActorItemUtil";
import ActorTypeUtil from "@/ActorTypeUtil";
import LangUtil from "@/LangUtil";

/**
 *太阳 物品合成 系统
 */
export default class SolarNewItemsFormulaState {

    static modelPath = "Abilities\\Spells\\Items\\AIam\\AIamTarget.mdl";
    /**
     * 自动将合成提示写入到演员物品说明里
     */
    static autoAddFormulaTips2ItemDescribe = true;
    /**
     * config
     */
    static config: {
        [id: string]: {
            materials?: string[],//ids
            material?: { [id: string]: number },//材料带数量
            formulaTips?: string,//合成文字提示: 新手剑 + 新手甲 = 新手套装
        }
    } = {}

    constructor() {
        //初始化数据 将只有id的材料设置为 需要的数量为1
        let config = SolarNewItemsFormulaState.config;
        for (let id in config) {
            let configData = config[id];
            if (configData && configData.materials) {
                configData.material = {}
                for (let materialID of configData.materials) {
                    configData.material[materialID] = 1;
                }
            }
        }

        if (SolarNewItemsFormulaState.autoAddFormulaTips2ItemDescribe) {
            SolarNewItemsFormulaState.generateFormulaTips();
        }


        //触发
        let trigger = new Trigger()
        trigger.registerAnyUnitEvent(EVENT_PLAYER_UNIT_PICKUP_ITEM)
        trigger.addAction(this.action)
    }

    static generateFormulaTips() {
        let config = SolarNewItemsFormulaState.config;
        //将材料数据推出 formulaTips
        for (let id in config) {
            let material = config[id]?.material;
            if (material && config[id].formulaTips == null) {
                let formulaTips = "";
                LangUtil.forEachSort(material, (itemId, v) => {
                    let itemName = SolarNewItemsFormulaState.getItemName(itemId);
                    if (material[itemId] > 1) {
                        itemName += "*" + material[itemId];
                    }
                    if (formulaTips.length > 0) {
                        formulaTips = formulaTips + " + "
                    }
                    formulaTips = formulaTips + itemName
                });
                formulaTips = formulaTips + " = " + SolarNewItemsFormulaState.getItemName(id);
                config[id].formulaTips = formulaTips;
            }
        }
        //
        for (let id in config) {
            let material = config[id]?.material;
            if (material && config[id].formulaTips) {
                let formulaTips = config[id].formulaTips
                for (let itemId in material) {
                    let itemActorType = ActorTypeUtil.getActorType(itemId);
                    if (itemActorType) {
                        let describe = itemActorType.describe || "";
                        if (!describe.includes(formulaTips)) {
                            describe = describe + "|n" + formulaTips;
                            itemActorType.describe = describe;
                        }
                    }
                }
                //
                let itemActorType = ActorTypeUtil.getActorType(id);
                if (itemActorType) {
                    let describe = itemActorType.describe || "";
                    if (!describe.includes(formulaTips)) {
                        describe = describe + "|n" + formulaTips;
                        itemActorType.describe = describe;
                    }
                }
            }
        }


    }


    static getItemName(itemId: string): string {
        let itemName = ActorTypeUtil.getActorType(itemId)?.name;
        if (itemName == null) {
            let iobj = _g_objs.item[itemId];
            if (iobj == null) {
                log.errorWithTraceBack("物品类型不存在ID=[" + tostring(itemId) + "]");
                return itemId;
            } else {
                itemName = iobj.Name;
            }
        }
        return itemName || itemId;
    }


    action(this: void) {
        let triggerUnit = Unit.fromEvent();
        //one case
        let config = SolarNewItemsFormulaState.config;
        for (let id in config) {
            let configData = config[id];
            let flag = SolarNewItemsFormulaState.NewItemsFormulaByMaterial(triggerUnit, id, configData.material);
            if (flag) {//如果合成了就退出
                return
            }
        }
    }


    static NewItemsFormulaByMaterial(unit: Unit, id: string, material: { [id: string]: number }): boolean {
        //检查是否满足材料
        let itemAndCharges = ActorItemUtil.getItemAndActorItemAndChargesFromUnit(unit.handle);
        for (const materialId in material) {
            let Charges = itemAndCharges[materialId]
            if (!Charges) {
                return false;
            }
            let materialCount = material[materialId];
            if (Charges < materialCount) {
                return false;
            }
        }
        //ok
        for (const materialId in material) {
            ActorItemUtil.costItemAndActorItemChargesFromUnit(unit.handle, materialId, material[materialId])
        }
        //create item
        if (ActorTypeUtil.hasActorType(id)) {
            //合成演员物品
            ActorItemUtil.addActorItemForUnit(id, unit.handle)
        } else {
            UnitAddItemById(unit.handle, id);
        }
        DestroyEffect(AddSpecialEffectTarget(SolarNewItemsFormulaState.modelPath, unit.handle, "origin"))
        return true;
    }


}